package com.zzw.system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.model.system.SysLoginLog;
import com.zzw.vo.system.SysLoginLogQueryVo;
import com.zzw.system.mapper.SysLoginLogMapper;
import com.zzw.system.service.SysLoginLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author zzw
 * @date 2023/4/26 10:08
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper,SysLoginLog> implements SysLoginLogService {
    //    记录用户登录的日志信息
    @Override
    public void record(String username, String ipaddr, Integer status, String msg) {
        SysLoginLog sysLoginLog=new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setStatus(status);
        sysLoginLog.setMsg(msg);
        baseMapper.insert(sysLoginLog);
    }
    //    获取登录日志分页列表
    @Override
    public IPage<SysLoginLog> selectPage(Page<SysLoginLog> loginLogPage, SysLoginLogQueryVo sysLoginLogQueryVo) {

//        获取条件值
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();
//        封装条件
        QueryWrapper<SysLoginLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.orderByDesc("id");
        if(!StringUtils.isEmpty(username)){
            queryWrapper.like("username",username);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            queryWrapper.gt("create_time",createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            queryWrapper.lt("create_time",createTimeEnd);
        }
        return baseMapper.selectPage(loginLogPage,queryWrapper);
    }
}
